package com.ez.supervise.action.biz;

import java.util.Calendar;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.GoalSelf;
import com.ez.supervise.service.GoalSelfService;
import com.ez.supervise.util.JsonUtils;

public class GoalSelfAction extends BaseJsonAction {

    private static final long serialVersionUID = -5237037468195860016L;
    private Long id;
    private GoalSelfService goalSelfService;
    private GoalSelf goalSelf;
    private Page page;
    private String selfStatus;//自荐指标状态(用于查询) audit-审核  apply 申请

    public String saveGoalSelf() {
	try {
	    if (isLogin()) {
		goalSelf.setOrgId(loginUser.getOrgId());
	    }
	    if (goalSelf.isNew()) {
		goalSelfService.insert(goalSelf);
	    } else {
		goalSelfService.update(goalSelf);
	    }
	    Result result = new Result(goalSelf.getId());
	    result.setEntity(goalSelf);
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new",
		"content" });
	    outString(JSONObject.fromObject(result, config).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String auditGoalSelf() {
	try {
	    GoalSelf dbGoalSelf = (GoalSelf) goalSelfService.get(goalSelf.getId());
	    dbGoalSelf.setAuditResult(goalSelf.getAuditResult());
	    dbGoalSelf.setAuditContent(goalSelf.getAuditContent());
	    if (isLogin()) {
		goalSelf.setAuditManId(loginUser.getUserId());
		goalSelf.setAuditOrgId(loginUser.getOrgId());
	    }
	    goalSelfService.auditGoalSelf(dbGoalSelf);
	    Result result = new Result(dbGoalSelf.getId());
	    result.setEntity(dbGoalSelf);
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new",
		"content" });
	    outString(JSONObject.fromObject(result, config).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    
    public String syncGoalSelf() {
	try {
	    int year = NumberUtils.isNumber(getRequest().getParameter("year")) ? Integer
			.valueOf(getRequest().getParameter("year")) : Calendar.getInstance().get(Calendar.YEAR);
	     
	    long orgId = NumberUtils.isNumber(getRequest().getParameter("orgId")) ? Long
		.valueOf(getRequest().getParameter("orgId")) : 0L;
	    goalSelfService.syncGoalSelf(year, orgId);
	    outString(RESULT_SUCCESS);
	
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAllGoalSelf() {
	try {
	    page = new Page();
	    List result = goalSelfService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));

	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findGoalSelfByPage() {
	try {
	    page = new Page();
	    page.putCondition("year", NumberUtils.isNumber(getRequest().getParameter("year")) ? Long
		.valueOf(getRequest().getParameter("year")) : null);
	    page.putCondition("orgId", NumberUtils.isNumber(getRequest().getParameter("orgId")) ? Long
		.valueOf(getRequest().getParameter("orgId")) : null);
	    page.putCondition("auditResult", getRequest().getParameter("auditResult"));
	    page.putCondition("auditOrgId", NumberUtils.isNumber(getRequest().getParameter("auditOrgId")) ? Long
	    		.valueOf(getRequest().getParameter("auditOrgId")) : null);
	    page.putCondition("type", getRequest().getParameter("type"));
	    page.putCondition("content", getRequest().getParameter("content"));
	    
	    if(StringUtils.isNotBlank(selfStatus)){
	    	page.putCondition("selfStatus", selfStatus);
	    } 
	    int start = NumberUtils.toInt(getRequest().getParameter("start"), 0);
	    int limit = NumberUtils.toInt(getRequest().getParameter("limit"), 20);
	    page.setStart(start++);
	    page.setLimit(limit);
	    page.addPageInfo();
	    goalSelfService.findGoalSelfByPage(page);
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));

	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String queryGoalSelfBySetting() {
	try {
	    page = new Page();
	    page.putCondition("year", NumberUtils.isNumber(getRequest().getParameter("year")) ? Long
		.valueOf(getRequest().getParameter("year")) : null);
	    page.putCondition("orgId", NumberUtils.isNumber(getRequest().getParameter("orgId")) ? Long
		.valueOf(getRequest().getParameter("orgId")) : null);

	    int start = NumberUtils.toInt(getRequest().getParameter("start"), 0);
	    int limit = NumberUtils.toInt(getRequest().getParameter("limit"), 20);
	    page.setStart(start++);
	    page.setLimit(limit);
	    page.addPageInfo();
	    goalSelfService.findGoalSelfByPage(page);
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));

	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAll() {
	try {
	    page = new Page();
	    List result = goalSelfService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));

	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     */
    public String deleteGoalSelf() {
	try {
	    if (id != null && id > 0) {
		goalSelfService.delete(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateGoalSelf() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strGoalSelfId = getRequest().getParameter("id");
	try {
	    if (id != null && id > 0) {
		GoalSelf saveGoalSelf = new GoalSelf();
		saveGoalSelf.setId(id);
		BeanUtils.setProperty(saveGoalSelf, fieldName, fieldValue);
		goalSelfService.update(saveGoalSelf);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public static void main(String[] args) {
	GoalSelf goalSelf = new GoalSelf();
	goalSelf.setId(1L);
	// goalSelf.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(goalSelf);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(goalSelf, config);
	System.out.println(jsonObj.toString());
    }

    public void setGoalSelfService(GoalSelfService goalSelfService) {
	this.goalSelfService = goalSelfService;
    }

    public GoalSelf getGoalSelf() {
	return goalSelf;
    }

    public void setGoalSelf(GoalSelf goalSelf) {
	this.goalSelf = goalSelf;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

	public void setSelfStatus(String selfStatus) {
		this.selfStatus = selfStatus;
	}

	public String getSelfStatus() {
		return selfStatus;
	}

}
